package com.ldc.common.mvp;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;

import me.jessyan.autosize.internal.CustomAdapt;

@Deprecated
/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseFragment<bind extends ViewBinding, P extends BasePresenter> extends Fragment implements IBaseView, CustomAdapt {
    protected final String TAG = this.getClass().getName();
    protected bind mBinding;
    protected P mPresenter;


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) {
            mPresenter.unBinding();
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = initViewBinding(container);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        try {
            mPresenter = initPresenter();
            if (null != mPresenter) {
                mPresenter.onBinding(this);
                getLifecycle().addObserver(mPresenter);//添加声明观察
            }
            initView();
            initData();
        } catch (Exception e) {
            show_error(e);
        }
    }

    protected abstract bind initViewBinding(@Nullable ViewGroup container);

    protected abstract int ui();

    protected abstract P initPresenter();

    protected abstract void initView();

    protected abstract void initData();

    protected void show_error(Exception e) {
        e.printStackTrace();
    }

}
